package com.web;

import com.web.kftsdk.common.util.sign.SignUtils;
import sun.misc.BASE64Decoder;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.StringReader;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

/**
 * Created by FDjavaone on 2018/7/19.
 */
public class TESTPFX {
    public static void main(String[] args) throws Exception {
        // System.out.println(PfxUtil.getPrivateKeyByPfx("D://319.pfx","nsyj0125A").getEncoded());
        // String json = "{\"g\":\"d\",\"a\":{\"b\":\"c\"},\"c\":\"d\",\"h\":[{\"b\":\"c\"}]}";


        String map = "a={\"b\":\"c\"}&c=d&g=d&h=[{\"b\":\"c\"}]";

        String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC1oovNOa+vL2dUM2NKR6vSJ/YwwbLWK5zivOKajAdAhY22bxW+deUutDclheLZG8jHYoMBP0ljk5JnQC+UWSax5qpnS85U1RSuEISLBjS9ep1DdJWYvMfnHVdkF3D18QLEUFtI6LVAmyLCEbdB1oJy/iRJdC0hx6lJR39j6WnJ6U0rDcPvJzCJP2vt6kptJyc32G1VlwNd7civpQ43ePz3eLl0Dy2JKhH4XCzGK7WKgzEUwhb2e/N++pM8VeVq3WWwvPNJ6FPhbxzbohGRtEqD59TuWIR5aR3cIZj7YBHpT2VWo+FbDLvqt7Q1jme6sOlTovghmMMWTyXxPQLmNB69AgMBAAECggEALt/I9miJt0sjfm5YlGXUqMKZXVHPpkfREA6w6DN096i/vrICSYnBYIEvriZUObVa5mG870h21xTRdoXxAXbUbprZpRjCIdBvqtAmdH5gg/mttifTJ3yoljNTR0hpZX8SOLmIJVOw2wBuQukxgvMp7SU7Fh2+O/Z78KZb8Nve6jU0w1WbSJKgtwNbBHs7KM44YZZFQMkC4dnm1q1yG/fucaFVDRB7iEsVrGFxkBXIssKEP1BX1FvAqaoNmJtY3YO+1P7I1EHUI1E1igxxLHnkF1fUNrBmbSGGzCwbtevwDUfnBeir62OQGQwfIaT49F3j9nDm61sQ3E12pkwlYoMSQQKBgQDhy0S037wVrBIcIGotMzSx9c6f2QlfrFTDwuMCdBYUj53NuHUOGPU6fMNo5g4KM8wZ7O4dHDqWvtHmqyDxHjyQFeVjbS4d/JemLDMULrYE25ML63AUJs2IqHLQn74SqOQmJ1zuJwgnifKHA1QPOa48t+J8JGuUZfM5YHLhXozYMQKBgQDN7vgT8lAJcpIx0foeOYw9aHG++FGVbBw9inlJ/1NPrAwp+3v2WMA/V0hz7scXz/A+tZIGZ7HyGBMhl0rhLy3O+qU4qVRpu8WLX2iF8o9YRYmxd3K0/OwEo8iTn2c9CN6UqYHc79JVvB1IO6+/srCpLr4OnDqTILIYfqTuSJqYTQKBgCoZ+MmxHiT8XLQZ5zJeZej46Hotwe2w+G04HZQLiUJ0NPxy9ICYbsFlGhqpKt0EI36CCzQqW1JYalMUfaP3+1ryej870GmO+8fkXfHZc98cAiMq108qnRBqOF5mKs7X5xKmLFHLvV2B/1Llb/sWykJQF08Gi3i0OdHtBf633pNBAoGAGMBhNtyH/Cq2IaDQRDO8hGD6Uu67t47v8gGjxF4Y4FmYeK9OXfSa3sIZ5j1L1rPad18Rdw2dmLCGFCKAEILAKcF8fEv15mN4QeEsyfS2f/CTtS3bLp+7qm8ZG40sNmzLe+jHwAtfJ4jdqCHsZMkQbLU+iLkEXUyvuzv1jOJTGn0CgYAAmmM5SEdY+VQwGgmGbq9s9PM2VJ3+0I1U5UFFvbb0Cua/obxOy+gG6+Yq3OiAwyE4RuD/v53Ikd9uwlT8Ts8vRWIPaU3E6Nd+QI26XKk3bS8Muv1B/w73nfXNAuFH7WXKIQk2uNlmQ5h5hfrEQ+LFZVyRlqNVGjvtb4+Z0+ZGMg==";

        System.out.println(SignUtils.valueOf("RSA").createSign(map, privateKey, "UTF-8"));


    }

    private static PublicKey getPublicKeyFromFile(String pubCerPath) {
        FileInputStream pubKeyStream = null;
        try {
            pubKeyStream = new FileInputStream(pubCerPath);
            byte[] e = new byte[pubKeyStream.available()];
            pubKeyStream.read(e);
            PublicKey var3 = findPublicKeyByText(new String(e));
            return var3;
        } catch (Exception var16) {
        } finally {
            if (pubKeyStream != null) {
                try {
                    pubKeyStream.close();
                } catch (Exception var14) {
                }
            }
        }
        return null;
    }

    private static PublicKey findPublicKeyByText(String pubKeyText) {
        try {
            CertificateFactory e = CertificateFactory.getInstance("X509");
            BufferedReader br = new BufferedReader(new StringReader(pubKeyText));
            String line;
            StringBuilder keyBuffer = new StringBuilder();
            while ((line = br.readLine()) != null) {
                if (!line.startsWith("-")) {
                    keyBuffer.append(line);
                }
            }
            Certificate certificate = e.generateCertificate(new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(keyBuffer.toString())));
            return certificate.getPublicKey();
        } catch (Exception var6) {
            return null;
        }
    }
}
